package com.example.fashion.android.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.example.fashion.android.R;

/**
 * Created by fashion on 2017/7/10.
 * 通知工具类
 */
public class NotifyUtil {
    //通知管理对象
    private static NotificationManager mNotificationManager;

    private static NotifyUtil mNotifyUtil;

    private NotifyUtil(Context context) {
        if (mNotificationManager == null) {
            mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
    }

    /**
     * 获得当前对象
     *
     * @param context
     * @return
     */
    public static NotifyUtil getInstance(Context context) {
        if (mNotifyUtil == null) {
            mNotifyUtil = new NotifyUtil(context);
        }
        return mNotifyUtil;
    }

    /**
     * 显示通知
     *
     * @param context
     * @param contentTitle
     * @param contentText
     * @param when
     * @param smallIcon
     * @param largeResourceId
     * @param intent
     * @param notifyId
     */
    public void toastNotify(Context context, String contentTitle, String contentText, long when, int smallIcon, int largeResourceId, PendingIntent intent,boolean isAutoCancel, int notifyId) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        if (!TextUtils.isEmpty(contentTitle)) {
            builder.setContentTitle(contentTitle);
        }

        if (!TextUtils.isEmpty(contentText)) {
            builder.setContentText(contentText);
        }
        if (when > 0) {
            builder.setWhen(when);
        }
        if (smallIcon > 0) {
            builder.setSmallIcon(smallIcon);
        }
        if (largeResourceId > 0) {
            builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), largeResourceId));
        }
        if (intent != null) {
            builder.setContentIntent(intent);
        }
        builder.setAutoCancel(isAutoCancel);
        Notification notification = builder.build();
        mNotificationManager.notify(notifyId, notification);
    }

}
